package com.xiaoshou.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xiaoshou.pojo.Answers;
import com.xiaoshou.pojo.Question;
import com.xiaoshou.pojo.User;
import com.xiaoshou.util.CreateTime;
import com.xiaoshou.util.MyUUID;
import com.xiaoshou.util.StringUtils;

@Service
@Transactional
public class AnswersService extends BaseService{

	public void saveNewAnswers(Answers answers,Question question,User user) {
		
		answers.setId(MyUUID.createUUID().toString());
		answers.setCreatetime(CreateTime.createTime());
		answers.setUpdatetime(answers.getCreatetime());
		answers.setState("未审核");
		answers.setContent(StringUtils.replaceHtml(answers.getContent()));
		answers.setUser(user);
		answers.setQuestion(question);
		answers.setLastexamine(user.getUsername());
		
		getAnswersDao().saveNewAnswers(answers);
		
	}

	public void updateAnswers(Answers answers) {
		Answers ans = getAnswersDao().findById(answers.getId());
		ans.setContent(StringUtils.replaceHtml(answers.getContent()));
		ans.setState(answers.getState());
		ans.setUpdatetime(CreateTime.createTime());
		if(answers.getLastexamine()!=null){
			ans.setLastexamine(StringUtils.replaceHtml(answers.getLastexamine()));
		}
		getAnswersDao().updateAnswer(ans);
		
	}

	public void deleteAnswerById(String answerId) {
		getAnswersDao().deleteAnswer(answerId);
	}

}
